home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / ripley / source / mpeg2dec.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  23KB  |  1,052 lines

  1.  
  2. /* mpeg2dec.c, main(), initialization, option processing                    */
  3.  
  4. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  5.  
  6. /*
  7.  * Disclaimer of Warranty
  8.  *
  9.  * These software programs are available to the user without any license fee or
  10.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  11.  * any and all warranties, whether express, implied, or statuary, including any
  12.  * implied warranties or merchantability or of fitness for a particular
  13.  * purpose.  In no event shall the copyright-holder be liable for any
  14.  * incidental, punitive, or consequential damages of any kind whatsoever
  15.  * arising from the use of these programs.
  16.  *
  17.  * This disclaimer of warranty extends to the user of these programs and user's
  18.  * customers, employees, agents, transferees, successors, and assigns.
  19.  *
  20.  * The MPEG Software Simulation Group does not represent or warrant that the
  21.  * programs furnished hereunder are free of infringement of any third-party
  22.  * patents.
  23.  *
  24.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  25.  * are subject to royalty fees to patent holders.  Many of these patents are
  26.  * general enough such that they are unavoidable regardless of implementation
  27.  * design.
  28.  *
  29.  */
  30.  
  31. #include "commandline.h"
  32. #include "mplay.h"
  33. #include <proto/dos.h>
  34. #include <proto/exec.h>
  35. #include <exec/memory.h>
  36.  
  37. #ifndef __PPC__
  38. #include <proto/asyncio.h>
  39. #else
  40. #include <powerup/ppclib/tasks.h>
  41. #endif
  42.  
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <ctype.h>
  48. #include <fcntl.h>
  49.  
  50. //#define GLOBAL
  51. #include "config.h"
  52. #include "global.h"
  53.  
  54.  
  55.  
  56. /* private prototypes */
  57. static int  video_sequence _ANSI_ARGS_((int *framenum));
  58. static int Decode_Bitstream _ANSI_ARGS_((void));
  59. static int  Headers _ANSI_ARGS_((void));
  60. static void Initialize_Sequence _ANSI_ARGS_((void));
  61. static void Initialize_Decoder _ANSI_ARGS_((void));
  62. static void Deinitialize_Sequence _ANSI_ARGS_((void));
  63. //static void Process_Options _ANSI_ARGS_((int argc, char *argv[]));
  64.  
  65.  
  66. #if OLD
  67. static int  Get_Val _ANSI_ARGS_((char *argv[]));
  68. #endif
  69.  
  70. /* #define DEBUG */
  71.  
  72. static void Clear_Options(void);
  73. #ifdef DEBUG
  74. static void Print_Options();
  75. #endif
  76.  
  77.  
  78.  
  79.  
  80.  
  81. static BOOL checkoptkeyword(int index, char *name)
  82. {
  83.     if (opts[index])
  84.     {
  85.         if (!stricmp((char *) opts[index], name))
  86.         {
  87.             return TRUE;
  88.         }
  89.     }
  90.     
  91.     return FALSE;
  92. }
  93.  
  94.  
  95.  
  96. int main(void)
  97. {
  98.     int ret, code;
  99.     void *commandLineHandle;
  100.  
  101.     if (InitGlobal())
  102.     {
  103.         commandLineHandle = ParseCommandLine();
  104.     
  105.         if (commandLineHandle)
  106.         {
  107.             Clear_Options();
  108.     
  109.             Main_Bitstream_Flag = 1;
  110.             Main_Bitstream_Filename = (char *) opts[OPT_FILE];
  111.  
  112.             
  113.             if (opts[OPT_SAVE])
  114.             {
  115.                 Output_Picture_Filename = (char *) opts[OPT_SAVE];            
  116.                 Output_Type = T_PPM;
  117.  
  118.                 if (opts[OPT_SAVEFORMAT])
  119.                 {
  120.                     if (!stricmp((char *) opts[OPT_SAVEFORMAT], "YUV"))
  121.                     {
  122.                         Output_Type = T_YUV;
  123.                     }
  124.                 }
  125.             }
  126.             else
  127.             {
  128.                 Output_Picture_Filename = "";
  129.                 Output_Type=T_X11;
  130.             }
  131.  
  132.  
  133.  
  134.             //    go!
  135.     
  136.     
  137.             ld = &base; /* select base layer context */
  138.             
  139.             /* open MPEG base layer bitstream file(s) */
  140.             /* NOTE: this is either a base layer stream or a spatial enhancement stream */
  141.  
  142. #ifdef __PPC__
  143.             if (!(base.Infile = PPCOpen(Main_Bitstream_Filename, MODE_OLDFILE)))
  144. //            if (!(base.Infile = Open(Main_Bitstream_Filename, O_RDONLY|O_BINARY)))
  145. #else
  146.             if (!(AsyncInfile = OpenAsync(Main_Bitstream_Filename, MODE_READ, inbufferlenght*2)))
  147. #endif
  148.  
  149.             {
  150.               fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
  151.               exit(1);
  152.             }
  153.     
  154. #ifdef __PPC__
  155.             if (base.Infile)
  156. #else
  157.             if (AsyncInfile)
  158. #endif
  159.             {
  160.  
  161. #ifdef __PPC__
  162.               base.inbuffer=PPCAllocVec(inbufferlenght,MEMF_ANY);
  163. #else
  164.               base.inbuffer=AllocVec(inbufferlenght,MEMF_ANY);
  165. #endif
  166.               
  167.               Initialize_Buffer(); 
  168.             
  169.               if(Show_Bits(8)==0x47)
  170.               {
  171.                 sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
  172.                 Error(Error_Text);
  173.               }
  174.             
  175.               next_start_code();
  176.               code = Show_Bits(32);
  177.             
  178.               switch(code)
  179.               {
  180.               case SEQUENCE_HEADER_CODE:
  181.                 break;
  182.               case PACK_START_CODE:
  183.                 System_Stream_Flag = 1;
  184.               case VIDEO_ELEMENTARY_STREAM:
  185.                 System_Stream_Flag = 1;
  186.                 break;
  187.               default:
  188.                 sprintf(Error_Text,"Unable to recognize stream type\n");
  189.                 Error(Error_Text);
  190.                 break;
  191.               }
  192.             
  193. #ifdef __PPC__
  194.                 PPCSeek(base.Infile, 0l, OFFSET_BEGINNING);
  195. //                lseek(base.Infile, 0l, 0);
  196. #else
  197.                 SeekAsync(AsyncInfile, 0, MODE_START);
  198. #endif
  199.               Initialize_Buffer(); 
  200.             }
  201.             
  202.             
  203.             //
  204.             //        MAIN LOOP!
  205.             //
  206.             //
  207.             
  208.             
  209.             do
  210.             {
  211. #ifdef __PPC__
  212.                 if(base.Infile)
  213. #else
  214.                 if(AsyncInfile)
  215. #endif
  216.                 {
  217. #ifdef __PPC__
  218.                   PPCSeek(base.Infile, 0l, OFFSET_BEGINNING);
  219. //                  lseek(base.Infile, 0l, 0);
  220. #else
  221.                   SeekAsync(AsyncInfile, 0, MODE_START);
  222. #endif
  223.                 }
  224.                 
  225.                 Initialize_Buffer(); 
  226.                 
  227.                 if(Two_Streams)
  228.                 {
  229.                   ld = &enhan; /* select enhancement layer context */
  230.                 
  231.                   if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)
  232.                   {
  233.                     sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
  234.                       Enhancement_Layer_Bitstream_Filename);
  235.                 
  236.                     Error(Error_Text);
  237.                   }
  238.                 
  239.                   Initialize_Buffer();
  240.                   ld = &base;
  241.                 }
  242.                 
  243.                 Initialize_Decoder();
  244.                 
  245.                 ret = Decode_Bitstream();
  246.     
  247.             } //while (!doneFlag && opts[OPT_LOOP] && (ret == FAULT_3));
  248.             while (!doneFlag && opts[OPT_LOOP]);
  249.  
  250. #if 0
  251.  
  252.  
  253. //    LOOP    doneflag    fault3
  254.  
  255. //  b        a            c
  256.  
  257. //    0        0x            0            ende
  258. //    1x        0x            0            weiter
  259. //    0        1            0            ende
  260. //    1x        1            0            ende
  261. //    0        0x            1            ende
  262. //    1x        0x            1            weiter
  263. //    0        1            1            ende
  264. //    1x        1            1            ende
  265.  
  266.         weiter: !a && b
  267.  
  268. #endif
  269.             
  270. #ifdef __PPC__
  271.             PPCClose(base.Infile);
  272. //            close(base.Infile);
  273. #else
  274.             CloseAsync(AsyncInfile);
  275. #endif            
  276.             if (Two_Streams)
  277.               close(enhan.Infile);
  278.             
  279.             return 0;
  280.     
  281.         }
  282.  
  283.         FreeCommandLine(commandLineHandle);
  284.         CloseGlobal();
  285.  
  286. #ifdef __PPC__
  287.         PPCFreeVec(base.inbuffer);
  288. #else
  289.         FreeVec(base.inbuffer);
  290. #endif
  291.  
  292.     }
  293.     else
  294.     {
  295.         printf("*** initialization failed.\n");
  296.     }
  297. }
  298.  
  299.  
  300.  
  301.  
  302.  
  303. #if 0
  304.   Clear_Options();
  305.  
  306.   /* decode command line arguments */
  307.   Process_Options(argc,argv);
  308.  
  309. #ifdef DEBUG
  310.   Print_Options();
  311. #endif
  312.  
  313.   ld = &base; /* select base layer context */
  314.  
  315.   /* open MPEG base layer bitstream file(s) */
  316.   /* NOTE: this is either a base layer stream or a spatial enhancement stream */
  317.   if ((base.Infile=open(Main_Bitstream_Filename,O_RDONLY|O_BINARY))<0)
  318.   {
  319.     fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
  320.     exit(1);
  321.   }
  322.  
  323.  
  324.   if(base.Infile != 0)
  325.   {
  326.     Initialize_Buffer(); 
  327.   
  328.     if(Show_Bits(8)==0x47)
  329.     {
  330.       sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
  331.       Error(Error_Text);
  332.     }
  333.  
  334.     next_start_code();
  335.     code = Show_Bits(32);
  336.  
  337.     switch(code)
  338.     {
  339.     case SEQUENCE_HEADER_CODE:
  340.       break;
  341.     case PACK_START_CODE:
  342.       System_Stream_Flag = 1;
  343.     case VIDEO_ELEMENTARY_STREAM:
  344.       System_Stream_Flag = 1;
  345.       break;
  346.     default:
  347.       sprintf(Error_Text,"Unable to recognize stream type\n");
  348.       Error(Error_Text);
  349.       break;
  350.     }
  351.  
  352.     lseek(base.Infile, 0l, 0);
  353.     Initialize_Buffer(); 
  354.   }
  355.  
  356.   if(base.Infile!=0)
  357.   {
  358.     lseek(base.Infile, 0l, 0);
  359.   }
  360.  
  361.   Initialize_Buffer(); 
  362.  
  363.   if(Two_Streams)
  364.   {
  365.     ld = &enhan; /* select enhancement layer context */
  366.  
  367.     if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)
  368.     {
  369.       sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
  370.         Enhancement_Layer_Bitstream_Filename);
  371.  
  372.       Error(Error_Text);
  373.     }
  374.  
  375.     Initialize_Buffer();
  376.     ld = &base;
  377.   }
  378.  
  379.   Initialize_Decoder();
  380.  
  381.   ret = Decode_Bitstream();
  382.  
  383.   close(base.Infile);
  384.  
  385.   if (Two_Streams)
  386.     close(enhan.Infile);
  387.  
  388.   return 0;
  389.  
  390. }
  391. #endif
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. /* IMPLEMENTAION specific rouintes */
  401. static void Initialize_Decoder()
  402. {
  403.   int i;
  404.  
  405.   /* Clip table */
  406.   if (!(Clip=(unsigned char *)malloc(1024)))
  407.     Error("Clip[] malloc failed\n");
  408.  
  409.   Clip += 384;
  410.  
  411.   for (i=-384; i<640; i++)
  412.     Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
  413.  
  414.   /* IDCT */
  415.   if (Reference_IDCT_Flag)
  416.     Initialize_Reference_IDCT();
  417.   else
  418.     Initialize_Fast_IDCT();
  419.  
  420. }
  421.  
  422. /* mostly IMPLEMENTAION specific rouintes */
  423. static void Initialize_Sequence()
  424. {
  425.   int cc, size;
  426.   static int Table_6_20[3] = {6,8,12};
  427.  
  428.   /* check scalability mode of enhancement layer */
  429.   if (Two_Streams && (enhan.scalable_mode!=SC_SNR) && (base.scalable_mode!=SC_DP))
  430.     Error("unsupported scalability mode\n");
  431.  
  432.   /* force MPEG-1 parameters for proper decoder behavior */
  433.   /* see ISO/IEC 13818-2 section D.9.14 */
  434.   if (!base.MPEG2_Flag)
  435.   {
  436.     progressive_sequence = 1;
  437.     progressive_frame = 1;
  438.     picture_structure = FRAME_PICTURE;
  439.     frame_pred_frame_dct = 1;
  440.     chroma_format = CHROMA420;
  441.     matrix_coefficients = 5;
  442.   }
  443.  
  444.   /* round to nearest multiple of coded macroblocks */
  445.   /* ISO/IEC 13818-2 section 6.3.3 sequence_header() */
  446.   mb_width = (horizontal_size+15)/16;
  447.   mb_height = (base.MPEG2_Flag && !progressive_sequence) ? 2*((vertical_size+31)/32)
  448.                                         : (vertical_size+15)/16;
  449.  
  450.   Coded_Picture_Width = 16*mb_width;
  451.   Coded_Picture_Height = 16*mb_height;
  452.  
  453.   /* ISO/IEC 13818-2 sections 6.1.1.8, 6.1.1.9, and 6.1.1.10 */
  454.   Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width
  455.                                            : Coded_Picture_Width>>1;
  456.   Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height
  457.                                             : Coded_Picture_Height>>1;
  458.   
  459.   /* derived based on Table 6-20 in ISO/IEC 13818-2 section 6.3.17 */
  460.   block_count = Table_6_20[chroma_format-1];
  461.  
  462.   for (cc=0; cc<3; cc++)
  463.   {
  464.     if (cc==0)
  465.       size = Coded_Picture_Width*Coded_Picture_Height;
  466.     else
  467.       size = Chroma_Width*Chroma_Height;
  468.  
  469.     if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size)))
  470.       Error("backward_reference_frame[] malloc failed\n");
  471.  
  472.     if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size)))
  473.       Error("forward_reference_frame[] malloc failed\n");
  474.  
  475.     if (!(auxframe[cc] = (unsigned char *)malloc(size)))
  476.       Error("auxframe[] malloc failed\n");
  477.  
  478.     if(Ersatz_Flag)
  479.       if (!(substitute_frame[cc] = (unsigned char *)malloc(size)))
  480.         Error("substitute_frame[] malloc failed\n");
  481.  
  482.  
  483.     if (base.scalable_mode==SC_SPAT)
  484.     {
  485.       /* this assumes lower layer is 4:2:0 */
  486.       if (!(llframe0[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
  487.         Error("llframe0 malloc failed\n");
  488.       if (!(llframe1[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
  489.         Error("llframe1 malloc failed\n");
  490.     }
  491.   }
  492.  
  493.   /* SCALABILITY: Spatial */
  494.   if (base.scalable_mode==SC_SPAT)
  495.   {
  496.     if (!(lltmp = (short *)malloc(lower_layer_prediction_horizontal_size*((lower_layer_prediction_vertical_size*vertical_subsampling_factor_n)/vertical_subsampling_factor_m)*sizeof(short))))
  497.       Error("lltmp malloc failed\n");
  498.   }
  499.  
  500. #ifdef DISPLAY
  501.   if (Output_Type==T_X11)
  502.   {
  503.     Initialize_Display_Process(FilePart(Main_Bitstream_Filename));
  504.  //   Initialize_Dither_Matrix();
  505.   }
  506. #endif /* DISPLAY */
  507.  
  508. }
  509.  
  510. void Error(text)
  511. char *text;
  512. {
  513.   fprintf(stderr,text);
  514.   exit(1);
  515. }
  516.  
  517. /* Trace_Flag output */
  518. void Print_Bits(code,bits,len)
  519. int code,bits,len;
  520. {
  521.   int i;
  522.   for (i=0; i<len; i++)
  523.     printf("%d",(code>>(bits-1-i))&1);
  524. }
  525.  
  526.  
  527.  
  528. #if 0
  529. /* option processing */
  530. static void Process_Options(argc,argv)
  531. int argc;                  /* argument count  */
  532. char *argv[];              /* argument vector */
  533. {
  534.   int i, LastArg, NextArg;
  535.  
  536.   /* at least one argument should be present */
  537.   if (argc<2)
  538.   {
  539.     printf("\n%s, %s\n",Version,Author);
  540.     printf("Usage:  mpeg2decode {options}\n\
  541. Options: -b  file  main bitstream (base or spatial enhancement layer)\n\
  542.          -cn file  conformance report (n: level)\n\
  543.          -e  file  enhancement layer bitstream (SNR or Data Partitioning)\n\
  544.          -f        store/display interlaced video in frame format\n\
  545.          -g        display in grey\n\
  546.          -in file  information & statistics report  (n: level)\n\
  547.          -l  file  file name pattern for lower layer sequence\n\
  548.                    (for spatial scalability)\n\
  549.          -on file  output format (0:YUV 1:SIF 2:TGA 3:PPM 4:X11 5:X11HiQ)\n\
  550.          -q        disable warnings to stderr\n\
  551.          -r        use double precision reference IDCT\n\
  552.          -t        enable low level tracing to stdout\n\
  553.          -u  file  print user_data to stdio or file\n\
  554.          -vn       verbose output (n: level)\n\
  555.          -x  file  filename pattern of picture substitution sequence\n\n\
  556. File patterns:  for sequential filenames, \"printf\" style, e.g. rec%%d\n\
  557.                  or rec%%d%%c for fieldwise storage\n\
  558. Levels:        0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n\
  559. Example:       mpeg2decode -b bitstream.mpg -f -r -o0 rec%%d\n\
  560.          \n");
  561.     exit(0);
  562.   }
  563.  
  564.  
  565.   Output_Type = -1;
  566.   i = 1;
  567.  
  568.   gray = 0;
  569.  
  570.   /* command-line options are proceeded by '-' */
  571.  
  572.   while(i < argc)
  573.   {
  574.     /* check if this is the last argument */
  575.     LastArg = ((argc-i)==1);
  576.  
  577.     /* parse ahead to see if another flag immediately follows current
  578.        argument (this is used to tell if a filename is missing) */
  579.     if(!LastArg)
  580.       NextArg = (argv[i+1][0]=='-');
  581.     else
  582.       NextArg = 0;
  583.  
  584.     /* second character, [1], after '-' is the switch */
  585.     if(argv[i][0]=='-')
  586.     {
  587.       switch(toupper(argv[i][1]))
  588.       {
  589.         /* third character. [2], is the value */
  590.       case 'B':
  591.         Main_Bitstream_Flag = 1;
  592.  
  593.         if(NextArg || LastArg)
  594.         {
  595.           printf("ERROR: -b must be followed the main bitstream filename\n");
  596.     }
  597.         else
  598.           Main_Bitstream_Filename = argv[++i]; 
  599.  
  600.         break;
  601.  
  602.  
  603.       case 'C':
  604.  
  605. #ifdef VERIFY
  606.         Verify_Flag = atoi(&argv[i][2]); 
  607.  
  608.         if((Verify_Flag < NO_LAYER) || (Verify_Flag > ALL_LAYERS))
  609.         {
  610.           printf("ERROR: -c level (%d) out of range [%d,%d]\n",
  611.             Verify_Flag, NO_LAYER, ALL_LAYERS);
  612.           exit(ERROR);
  613.         }
  614. #else  /* VERIFY */
  615.         printf("This program not compiled for Verify_Flag option\n");
  616. #endif /* VERIFY */
  617.         break;
  618.  
  619.       case 'E':
  620.         Two_Streams = 1; /* either Data Partitioning (DP) or SNR Scalability enhancment */
  621.                        
  622.         if(NextArg || LastArg)
  623.         {
  624.           printf("ERROR: -e must be followed by filename\n");
  625.           exit(ERROR);
  626.         }
  627.         else
  628.           Enhancement_Layer_Bitstream_Filename = argv[++i]; 
  629.  
  630.         break;
  631.  
  632.  
  633.       case 'F':
  634.         Frame_Store_Flag = 1;
  635.         break;
  636.  
  637.       case 'G':
  638.         gray = 1;        // bild in grau anzeigen
  639.         break;
  640.  
  641.  
  642.       case 'I':
  643. #ifdef VERIFY
  644.         Stats_Flag = atoi(&argv[i][2]); 
  645. #else /* VERIFY */
  646.         printf("WARNING: This program not compiled for -i option\n");
  647. #endif /* VERIFY */     
  648.         break;
  649.     
  650.       case 'L':  /* spatial scalability flag */
  651.         Spatial_Flag = 1;
  652.  
  653.        if(NextArg || LastArg)
  654.        {
  655.          printf("ERROR: -l must be followed by filename\n");
  656.          exit(ERROR);
  657.        }
  658.        else
  659.          Lower_Layer_Picture_Filename = argv[++i]; 
  660.  
  661.         break;
  662.  
  663.       case 'O':
  664.   
  665.         Output_Type = atoi(&argv[i][2]); 
  666.     dnormal = 0;  
  667.  
  668.         if((Output_Type==4) || (Output_Type==5))
  669.           Output_Picture_Filename = "";  /* no need of filename */
  670.         else if(NextArg || LastArg)  
  671.         {
  672.           printf("ERROR: -o must be followed by filename\n");
  673.           exit(ERROR);
  674.         }
  675.         else
  676.         /* filename is separated by space, so it becomes the next argument */
  677.           Output_Picture_Filename = argv[++i]; 
  678.  
  679. #ifdef DISPLAY
  680.         if (Output_Type==T_X11HIQ)
  681.         {
  682.           dnormal = 1;    //hiQdither = 1;
  683.           Output_Type=T_X11;
  684.         }
  685. #endif /* DISPLAY */
  686.         break;
  687.  
  688.       case 'Q':
  689.         Quiet_Flag = 1;
  690.         break;
  691.  
  692.       case 'R':
  693.         Reference_IDCT_Flag = 1;
  694.         break;
  695.     
  696.       case 'T':
  697. #ifdef TRACE
  698.         Trace_Flag = 1;
  699. #else /* TRACE */
  700.         printf("WARNING: This program not compiled for -t option\n");
  701. #endif /* TRACE */
  702.         break;
  703.  
  704.       case 'U':
  705.         User_Data_Flag = 1;
  706.  
  707.       case 'V':
  708. #ifdef VERBOSE
  709.         Verbose_Flag = atoi(&argv[i][2]); 
  710. #else /* VERBOSE */
  711.         printf("This program not compiled for -v option\n");
  712. #endif /* VERBOSE */
  713.         break;
  714.  
  715.  
  716.       case 'X':
  717.         Ersatz_Flag = 1;
  718.  
  719.        if(NextArg || LastArg)
  720.        {
  721.          printf("ERROR: -x must be followed by filename\n"); 
  722.          exit(ERROR);
  723.        }
  724.        else
  725.         Substitute_Picture_Filename = argv[++i]; 
  726.  
  727.         break;
  728.  
  729.  
  730.  
  731.       default:
  732.         fprintf(stderr,"undefined option -%c ignored. Exiting program\n", 
  733.           argv[i][1]);
  734.  
  735.         exit(ERROR);
  736.     
  737.       } /* switch() */
  738.     } /* if argv[i][0] == '-' */
  739.     
  740.     i++;
  741.  
  742.       /* check for bitstream filename argument (there must always be one, at the very end
  743.        of the command line arguments */
  744.  
  745.   } /* while() */
  746.  
  747.  
  748.   /* options sense checking */
  749.  
  750.   if(Main_Bitstream_Flag!=1)
  751.   {
  752.     printf("There must be a main bitstream specified (-b filename)\n");
  753.   }
  754.  
  755.   /* force display process to show frame pictures */
  756.   if((Output_Type==4 || Output_Type==5) && Frame_Store_Flag)
  757.     Display_Progressive_Flag = 1;
  758.   else
  759.     Display_Progressive_Flag = 0;
  760.  
  761. #ifdef VERIFY
  762.   /* parse the bitstream, do not actually decode it completely */
  763.   
  764.  
  765. #if 0
  766.   if(Output_Type==-1)
  767.   {
  768.     Decode_Layer = Verify_Flag;
  769.     printf("FYI: Decoding bitstream elements up to: %s\n", 
  770.       Layer_Table[Decode_Layer]);
  771.   }
  772.   else
  773. #endif
  774.     Decode_Layer = ALL_LAYERS;
  775.  
  776. #endif /* VERIFY */
  777.  
  778.   /* no output type specified */
  779.   if(Output_Type==-1)
  780.   {
  781.     Output_Type = 9; 
  782.     Output_Picture_Filename = "";
  783.   }
  784.  
  785.  
  786. #ifdef DISPLAY
  787.   if (Output_Type==T_X11)
  788.   {
  789.     if(Frame_Store_Flag)
  790.       Display_Progressive_Flag = 1;
  791.     else
  792.       Display_Progressive_Flag = 0;
  793.  
  794.     Frame_Store_Flag = 1; /* to avoid calling dither() twice */
  795.   }
  796. #endif
  797.  
  798.  
  799. }
  800.  
  801. #endif
  802.  
  803.  
  804.  
  805. #ifdef OLD
  806. /* 
  807.    this is an old routine used to convert command line arguments
  808.    into integers 
  809. */
  810. static int Get_Val(argv)
  811. char *argv[];
  812. {
  813.   int val;
  814.  
  815.   if (sscanf(argv[1]+2,"%d",&val)!=1)
  816.     return 0;
  817.  
  818.   while (isdigit(argv[1][2]))
  819.     argv[1]++;
  820.  
  821.   return val;
  822. }
  823. #endif
  824.  
  825.  
  826.  
  827. static int Headers()
  828. {
  829.   int ret;
  830.  
  831.   ld = &base;
  832.   
  833.  
  834.   /* return when end of sequence (0) or picture
  835.      header has been parsed (1) */
  836.  
  837.   ret = Get_Hdr();
  838.  
  839.  
  840.   if (Two_Streams)
  841.   {
  842.     ld = &enhan;
  843.     if (Get_Hdr()!=ret && !Quiet_Flag)
  844.       fprintf(stderr,"streams out of sync\n");
  845.     ld = &base;
  846.   }
  847.  
  848.   return ret;
  849. }
  850.  
  851.  
  852.  
  853. static int Decode_Bitstream()
  854. {
  855.   int ret;
  856.   int Bitstream_Framenum;
  857.  
  858.   Bitstream_Framenum = 0;
  859.  
  860.   for(;;)
  861.   {
  862.  
  863. #ifdef VERIFY
  864.     Clear_Verify_Headers();
  865. #endif /* VERIFY */
  866.  
  867.     ret = Headers();
  868.     
  869.     if(ret==1)
  870.     {
  871.       ret = video_sequence(&Bitstream_Framenum);
  872.     }
  873.     else
  874.       return(ret);
  875.  
  876.     if (doneFlag || ret == FAULT_3)
  877.     {
  878.         return(ret);
  879.     }
  880.  
  881.  
  882.   }
  883.  
  884. }
  885.  
  886.  
  887. static void Deinitialize_Sequence()
  888. {
  889.   int i;
  890.  
  891.   /* clear flags */
  892.   base.MPEG2_Flag=0;
  893.  
  894.   for(i=0;i<3;i++)
  895.   {
  896.     free(backward_reference_frame[i]);
  897.     free(forward_reference_frame[i]);
  898.     free(auxframe[i]);
  899.  
  900.     if (base.scalable_mode==SC_SPAT)
  901.     {
  902.      free(llframe0[i]);
  903.      free(llframe1[i]);
  904.     }
  905.   }
  906.  
  907.   if (base.scalable_mode==SC_SPAT)
  908.     free(lltmp);
  909.  
  910. #ifdef DISPLAY
  911.   if (Output_Type==T_X11) 
  912.     Terminate_Display_Process();
  913. #endif
  914. }
  915.  
  916.  
  917. static int video_sequence(Bitstream_Framenumber)
  918. int *Bitstream_Framenumber;
  919. {
  920.   int Bitstream_Framenum;
  921.   int Sequence_Framenum;
  922.   int Return_Value;
  923.  
  924.   Bitstream_Framenum = *Bitstream_Framenumber;
  925.   Sequence_Framenum=0;
  926.  
  927.   Initialize_Sequence();
  928.  
  929.   /* decode picture whose header has already been parsed in 
  930.      Decode_Bitstream() */
  931.     
  932.     if (!doneFlag)
  933.     {
  934.  
  935.  
  936.     Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
  937.     if (Fault_Flag == FAULT_3)
  938.     {
  939.         Return_Value = FAULT_3;
  940.     }
  941.  
  942.  
  943.   /* update picture numbers */
  944.   if (!Second_Field)
  945.   {
  946.     Bitstream_Framenum++;
  947.     Sequence_Framenum++;
  948.   }
  949.  
  950.  
  951.   /* loop through the rest of the pictures in the sequence */
  952.  
  953.   while ((Return_Value=Headers()))
  954.   {
  955.     Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
  956.     if (Fault_Flag == FAULT_3)
  957.     {
  958.         Return_Value = FAULT_3;
  959.         break;
  960.     }
  961.  
  962.     if (doneFlag == 1)
  963.     {
  964.         break; 
  965.     }
  966.  
  967.    if (!Second_Field)
  968.     {
  969.       Bitstream_Framenum++;
  970.       Sequence_Framenum++;
  971.     }
  972.   }
  973.  
  974.   /* put last frame */
  975.  
  976.     if (!doneFlag) 
  977.     {
  978.         if (Sequence_Framenum!=0)
  979.         {
  980.             Output_Last_Frame_of_Sequence(Bitstream_Framenum);
  981.          }
  982.       }
  983.  
  984.     }
  985.  
  986.   Deinitialize_Sequence();
  987.  
  988. #ifdef VERIFY
  989.     Clear_Verify_Headers();
  990. #endif /* VERIFY */
  991.  
  992.   *Bitstream_Framenumber = Bitstream_Framenum;
  993.   return(Return_Value);
  994. }
  995.  
  996.  
  997.  
  998. static void Clear_Options()
  999. {
  1000.   Verbose_Flag = 0;
  1001.   Output_Type = 0;
  1002.   Output_Picture_Filename = " ";
  1003.   hiQdither  = 0;
  1004.   Output_Type = 0;
  1005.   Frame_Store_Flag = 0;
  1006.   Spatial_Flag = 0;
  1007.   Lower_Layer_Picture_Filename = " ";
  1008.   Reference_IDCT_Flag = 0;
  1009.   Trace_Flag = 0;
  1010.   Quiet_Flag = 0;
  1011.   Ersatz_Flag = 0;
  1012.   Substitute_Picture_Filename  = " ";
  1013.   Two_Streams = 0;
  1014.   Enhancement_Layer_Bitstream_Filename = " ";
  1015.   Big_Picture_Flag = 0;
  1016.   Main_Bitstream_Flag = 0;
  1017.   Main_Bitstream_Filename = " ";
  1018.   Verify_Flag = 0;
  1019.   Stats_Flag  = 0;
  1020.   User_Data_Flag = 0; 
  1021. }
  1022.  
  1023.  
  1024. #ifdef DEBUG
  1025. static void Print_Options()
  1026. {
  1027.   
  1028.   printf("Verbose_Flag                         = %d\n", Verbose_Flag);
  1029.   printf("Output_Type                          = %d\n", Output_Type);
  1030.   printf("Output_Picture_Filename              = %s\n", Output_Picture_Filename);
  1031.   printf("hiQdither                            = %d\n", hiQdither);
  1032.   printf("Output_Type                          = %d\n", Output_Type);
  1033.   printf("Frame_Store_Flag                     = %d\n", Frame_Store_Flag);
  1034.   printf("Spatial_Flag                         = %d\n", Spatial_Flag);
  1035.   printf("Lower_Layer_Picture_Filename         = %s\n", Lower_Layer_Picture_Filename);
  1036.   printf("Reference_IDCT_Flag                  = %d\n", Reference_IDCT_Flag);
  1037.   printf("Trace_Flag                           = %d\n", Trace_Flag);
  1038.   printf("Quiet_Flag                           = %d\n", Quiet_Flag);
  1039.   printf("Ersatz_Flag                          = %d\n", Ersatz_Flag);
  1040.   printf("Substitute_Picture_Filename          = %s\n", Substitute_Picture_Filename);
  1041.   printf("Two_Streams                          = %d\n", Two_Streams);
  1042.   printf("Enhancement_Layer_Bitstream_Filename = %s\n", Enhancement_Layer_Bitstream_Filename);
  1043.   printf("Big_Picture_Flag                     = %d\n", Big_Picture_Flag);
  1044.   printf("Main_Bitstream_Flag                  = %d\n", Main_Bitstream_Flag);
  1045.   printf("Main_Bitstream_Filename              = %s\n", Main_Bitstream_Filename);
  1046.   printf("Verify_Flag                          = %d\n", Verify_Flag);
  1047.   printf("Stats_Flag                           = %d\n", Stats_Flag);
  1048.   printf("User_Data_Flag                       = %d\n", User_Data_Flag);
  1049.  
  1050. }
  1051. #endif
  1052.